import java.util.*;


public class CollectionTest {
   private static final int NUM_ITEMS = 25000;
   private static int[] randomNumbers = null;
   private static Object[] objects = new Object[NUM_ITEMS];

   static {
      randomNumbers = new int[100];
      Random rand = new Random();
      for (int i = 0; i < randomNumbers.length; i++) {
         randomNumbers[i] = rand.nextInt(NUM_ITEMS);
      }
   }

   static {
      for (int i = 0; i < NUM_ITEMS; i++) {
         objects[i] = new Integer(i);
      }
   }


   private static int randCounter = 0;

   private static int random() {
      randCounter++;
      return randomNumbers[randCounter % randomNumbers.length];

   }
   public static void main(String[] args) {

      Collection arrayList = new ArrayList();
      test(arrayList);
      Collection linkedList = new LinkedList();
      test(linkedList);
      Collection vector = new Vector();
      test(vector);
      Collection treeSet = new TreeSet();
      test(treeSet);
      Collection hashSet = new HashSet();
      test(hashSet);     

   }

   public static void test(Collection c) {
      System.out.println("Testing "+c.getClass());
      Stopwatch timer = new Stopwatch().start();
      add(c);
      timer.stop();
      System.out.println("add: "+ timer.getElapsedTime());
      timer.reset().start();
      iterate(c);
      timer.stop();     
      System.out.println("iter: "+ timer.getElapsedTime());
      if (c instanceof List) {
         List l = (List)c;
         timer.reset().start();
         randomAccess(l);
         timer.stop();
         System.out.println("random: "+ timer.getElapsedTime());        
      }
      timer.reset().start();      
      remove(c);
      timer.stop();
      System.out.println("remove: "+ timer.getElapsedTime());

   }
   public static void add(Collection c) {
      for (int i = 0; i < NUM_ITEMS; i++) {
         c.add(objects[i]);
      }
   }

   public static void iterate(Collection c) {
      for (int i = 0; i < 100; i++) {
         Iterator iter = c.iterator();
         while (iter.hasNext()) {
            Object o = iter.next();
         }
      }
   }

   public static void remove(Collection c) {
      Iterator iter = c.iterator();
      while (iter.hasNext()) {
         Object o = iter.next();
         iter.remove();
      }
   }

   public static void randomAccess(List c) {
      for (int i = 0; i < NUM_ITEMS; i++) {
         Object o = c.get(random());
      }
   }

}



